from bs4 import *
from bs4.element import *

# BeautifulSoup的使用

str = '''
<title id="id">Title</title>
<div class="info" style="float: left;">Welcome to LeiYang</div>
<div class="info" style="float: right;">
    <span>Good city</span>
    <a href="https://www.baidu.com">百度</a>
    <i><!--注释--></i>
</div>
'''
soup = BeautifulSoup(str, "lxml")
print(soup.title)
print(soup.div)
print(soup.div.attrs)
print(soup.div.get("class"))
print(soup.div["style"])
print(soup.a["href"])
print(soup.a.get("href"))
print(soup.div.text)
print(soup.div.string)
if type(soup.i.string) == Comment:
    print(soup.i.string)
    print(soup.i.prettify())
else:
    print(soup.i.text)
print("------------------find_all---------------------")
print(soup.find_all("div"))
print(soup.find_all(id="id"))
print(soup.find_all(class_="info"))
print(soup.find_all(attrs={"style": "float: left;"}))
print("------------------css---------------------")
print(soup.select("title"))
print(soup.select("#id"))
print(soup.select(".info"))
print(soup.select("div span"))
print(soup.select("div > span"))
print(soup.select("div")[1].select("a"))
print(soup.select("div")[0].text)
